package org.example.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.example.domain.dto.ErpDepotItemDTO;
import org.example.domain.po.*;
import org.example.service.ErpPersonService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/basic/person")
public class ErpPersonController {

    @Autowired
    ErpPersonService erpPersonService;

    @GetMapping("/find")
    public Result<Page<ErpPerson>> findByPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer size,
            @RequestAttribute("user") ErpUser user,
            String perName,
            String type
    ){
        Page<ErpPerson> page = new Page<>(current, size);
        page = erpPersonService.findByPage(page, perName, type, user.getTenantId());
        return Result.success(page);
    }

    /**
     * 经手人新增
     * @param erpPerson
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestAttribute("user") ErpUser user, @RequestBody ErpPerson erpPerson){
        erpPerson.setTenantId(Math.toIntExact(user.getTenantId()));
        erpPerson.setStatus(1);
        return erpPersonService.save(erpPerson) ? Result.success("新增成功！") : Result.error("新增失败！");
    }

    /**
     * 修改
     * @param erpPerson
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody ErpPerson erpPerson){
        return erpPersonService.updateById(erpPerson) ? Result.success("编辑成功！") : Result.error("编辑失败！");
    }

    /**
     * 根据id删除
     * @param depId
     * @return
     */
    @DeleteMapping("/{depId}")
    public Result<String> remove(@PathVariable("depId") Integer depId){
        return erpPersonService.removeById(depId) ? Result.success("删除成功！") : Result.error("删除失败！");
    }

    /**
     * 批量删除
     * @param erpPersons
     * @return
     */
    @PostMapping("/batch")
    public Result removeAll(@RequestBody List<ErpPerson> erpPersons){
        return erpPersonService.removeBatchByIds(erpPersons) ? Result.success("操作成功！") : Result.error("操作失败！");
    }

    /**
     * 批量修改
     * @param modify
     * @return
     */
    @PutMapping("/modifyTheState")
    public Result<String> modifyTheState(@RequestBody UpdateStatusRequest modify){
        boolean theState = erpPersonService.modifyTheState(modify.getIds(), modify.getStatus());
        return theState ? Result.success("操作成功！") : Result.error("操作失败！");
    }
    @GetMapping("/list")
    public R<List<ErpPerson>> getPersonById() {
        return R.success(erpPersonService.selectByExample());
    }
    @PostMapping("/add")
    public R addAccount(@RequestBody ErpPerson erpPerson) {
        int result = erpPersonService.insert(erpPerson.getPerName(), erpPerson.getType(), erpPerson.getSort(), erpPerson.getStatus(), erpPerson.getTenantId());

        if (result > 0) {
            return R.success("添加成功");
        }
        return R.fail("添加失败");
    }

}
